<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive">
      <!-- 按条件替换 -->
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-active"></slot>  
    </div>    
    <!-- <div :class="{active: isActive}">
      <slot name="item-text"></slot>
    </div> -->
    <!-- <div :class="[isActive ? 'active' : '']">
      <slot name="item-text"></slot>
    </div> -->
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>

  </div>
</template>

<script>
export default {
  data() {
    return {
      // isActive: true
    }
  },
  // 从父组件中传入path和想要激活时的颜色
  props: {
    path: String,
    activeColor: {
      type: String,
      default: 'red'
    }
  },
  computed: {
    isActive() {
      return this.$route.path.indexOf(this.path) !== -1
    },
    activeStyle() {
      // 如果激活了，就把激活时的颜色返回，否则返回一个空的对象
      return this.isActive ? {color: this.activeColor} : {}
    }
  },
  methods: {
    itemClick() {
      // 用catch来解决了同一个导航点击多次会报错的问题
      this.$router.push(this.path).catch(err => err)
      // 这里好像有一个bug，第一次的点击的时候path是上一个路径
      // console.log(this.$route.path);
    }
  },
}
</script>

<style scoped>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
  }

  .tab-bar-item img{
    width: 24px;
    height: 24px;
    margin-top: 3px;
    margin-bottom: 2px;
    vertical-align: middle;
  }

  .active {
    color: red;
  }
</style>